package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import dbutil.DBHelper;
import entity.Admin;
import entity.AdminPrivs;



public class AdminDao {
	private boolean isSuccess;

	// 删除一个管理员
	public boolean delAdmin(String adminName) throws SQLException {
		PreparedStatement pstmt = null;
		Connection conn = null;
		isSuccess = false;
		try {
			conn = DBHelper.getConnection();
			String sql = "delete from admins where adminName = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, adminName);
			int i = pstmt.executeUpdate();
			if(i > 0) {
				isSuccess = true;
			}
			return isSuccess;
		} catch (SQLException e) {
			e.printStackTrace();
			return isSuccess;
		} finally {
			pstmt.close();
		}
	}
	
	// 根据用户名和密码查询用户
	public Admin getAdminByNP(String adminname, String password) throws SQLException {
		Connection conn = DBHelper.getConnection();
		String sql = "select * from admins where adminName = ? and password = ?;";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, adminname);
		pstmt.setString(2, password);
		ResultSet rs = pstmt.executeQuery();
		
		if (rs.next()) {// 存在用户
			Admin admin = new Admin();
			admin.setAdminName(rs.getString("adminName"));
			admin.setDeptName(rs.getString("deptName"));
			admin.setCnName(rs.getString("cnName"));
			admin.setCreateDate(rs.getDate("createDate"));
			admin.setDeptName(rs.getString("deptName"));
			admin.setEmail(rs.getString("email"));
			admin.setIsSuper(rs.getString("isSuper"));
			admin.setPassword(rs.getString("password"));
			admin.setPhone(rs.getString("phone"));
			admin.setStatus(rs.getString("status"));
			
			return admin;
		}
		
		return null;
	}

	// 查询所有管理员	
	public List<Admin> queryAllAdmins() throws SQLException {
		Connection conn = DBHelper.getConnection();
		String sql = "select * from admins";
		Statement stmt = (Statement) conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		List<Admin> list = new ArrayList<Admin>();
		Admin admin = null;
		
		while(rs.next()) {
			admin = new Admin();
			admin.setAdminName(rs.getString("adminName"));
			admin.setDeptName(rs.getString("deptName"));
			admin.setPassword(rs.getString("password"));
			admin.setCnName(rs.getString("cnName"));
			admin.setPhone(rs.getString("phone"));
			admin.setStatus(rs.getString("status"));
			admin.setCreateDate(rs.getDate("createDate"));
			admin.setIsSuper(rs.getString("isSuper"));
			admin.setEmail(rs.getString("email"));
			list.add(admin);
		}
		stmt.close();
		rs.close();
		
		return list;
	}
	
	// 根据用户名称查询管理员拥有的权限
	public AdminPrivs queryAdminPrivs(String adminName) throws SQLException {
		Connection conn = DBHelper.getConnection();
		String sql = "select * from adminprivs where adminName = ?";
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, adminName);
		ResultSet rs = pstmt.executeQuery();
		Map<String, String> privNameAddUser = new HashMap<String, String>();
		Map<String, Date> privNameDate = new HashMap<String, Date>();
		String priName = "";
		
		AdminPrivs adminPrivs = new AdminPrivs();
		adminPrivs.setAdminName(adminName);
		while(rs.next()) {
			priName = rs.getString("priName");
			privNameAddUser.put(priName, rs.getString("addUser"));
			privNameDate.put(priName, rs.getDate("addDate"));
		}
		adminPrivs.setPrivNameAddUser(privNameAddUser);
		adminPrivs.setPrivNameDate(privNameDate);
		pstmt.close();
		rs.close();
		
		return adminPrivs;
	}
	
	// 添加管理员
	public boolean addAdmin(Admin admin) throws SQLException {
		PreparedStatement pstmt = null;
		Connection conn = null;
		isSuccess = false;
		try {
			conn = DBHelper.getConnection();
			String sql ="insert into admins(adminName,deptName,password,cnName,phone,status,createDate,isSuper,email) values (?,?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, admin.getAdminName());
			pstmt.setString(2, admin.getDeptName());
			pstmt.setString(3, admin.getPassword());
			pstmt.setString(4, admin.getCnName());
			pstmt.setString(5, admin.getPhone());
			pstmt.setString(6, admin.getStatus());
			pstmt.setDate(7, new java.sql.Date(admin.getCreateDate().getTime()));
			pstmt.setString(8, admin.getIsSuper());
			pstmt.setString(9, admin.getEmail());
			int i = pstmt.executeUpdate();
			
			if (i > 0) {
				isSuccess = true;
			}
			
			return isSuccess;
		} catch (SQLException e) {
			e.printStackTrace();
			return isSuccess;
		} finally {
			pstmt.close();
		}
	}
	
	// 修改管理员
	public boolean editAdmin(Admin admin) throws SQLException {
		PreparedStatement pstmt = null;
		Connection conn = null;
		isSuccess = false;
		try {
			conn = DBHelper.getConnection();
			String sql ="update admins set deptName=?,password=?,cnName=?,phone=?,isSuper=?,email=? where adminName = ?";
			pstmt = conn.prepareStatement(sql);
			// pstmt.setString(1, admin.getAdminName());
			pstmt.setString(1, admin.getDeptName());
			pstmt.setString(2, admin.getPassword());
			pstmt.setString(3, admin.getCnName());
			pstmt.setString(4, admin.getPhone());
			//pstmt.setString(6, admin.getStatus());
			pstmt.setString(5, admin.getIsSuper());
			pstmt.setString(6, admin.getEmail());
			pstmt.setString(7, admin.getAdminName());
			int i = pstmt.executeUpdate();
			
			if (i > 0) {
				isSuccess = true;
			}
			return isSuccess;
		} catch (SQLException e) {
			e.printStackTrace();
			return isSuccess;
		} finally {
			pstmt.close();
		}
		
	}
	
	// 添加管理员权限
	public boolean addAdminPrivs(AdminPrivs adminPrivs) throws SQLException {
		Set<String> privName = null;
		Map<String, String> privNameAddUser = null;
		Map<String, Date> privNameDate = null;
		PreparedStatement pstmt = null;
		isSuccess = false;
		try {
			Connection conn = DBHelper.getConnection();
			StringBuffer sql= new StringBuffer("insert into adminprivs (adminName,priName,addUser,addDate) values ");
			privNameAddUser = adminPrivs.getPrivNameAddUser();
			privNameDate = adminPrivs.getPrivNameDate();
			privName = privNameAddUser.keySet();
			for(String pName : privName) {
				sql.append("('").append(adminPrivs.getAdminName()).append("','");
				sql.append(pName).append("','");
				sql.append(privNameAddUser.get(pName)).append("','");
				sql.append(new java.sql.Date(privNameDate.get(pName).getTime())).append("'),");
			}
			sql.deleteCharAt(sql.length() - 1);
			pstmt = conn.prepareStatement(sql.toString());
			int i = pstmt.executeUpdate();
			pstmt.close();
			if (i > 0) {
				isSuccess = true;
			}
			return isSuccess;
		} catch (SQLException e) {
			e.printStackTrace();
			return isSuccess;
		} finally {
			pstmt.close();
		}
	}
	
	// 删除管理员权限
	public boolean delAdminPrivs(String[] delPrivArr, String adminName) throws SQLException {
		StringBuffer sql = null;
		PreparedStatement pstmt = null;
		Connection conn = null;
		isSuccess = false;
		try {
			conn = DBHelper.getConnection();
			sql = new StringBuffer("delete from adminprivs where adminName = '").append(adminName).append("' and priName in (");
			for (int i = 0; i < delPrivArr.length; i++) {
				sql.append("'").append(delPrivArr[i]).append("',");
			}
			sql.deleteCharAt(sql.length() - 1).append(")");
			pstmt = conn.prepareStatement(sql.toString());
			int i = pstmt.executeUpdate();
			
			if (i > 0) {
				isSuccess = true;
			}
			
			return isSuccess;
		} catch (SQLException e) {
			e.printStackTrace();
			return isSuccess;
		} finally {
			pstmt.close();
		}
		
	}
	
	public boolean editAdminStatus(String status, String name) throws SQLException {
		String sql = "update admins set status = ? where adminName = ?";
		PreparedStatement pstmt = null;
		Connection conn = null;
		isSuccess = false;
		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, status);
			pstmt.setString(2, name);
			int i = pstmt.executeUpdate();
			
			if (i > 0) {
				isSuccess = true;
			}
			
			return isSuccess;
		} catch (SQLException e) {
			e.printStackTrace();
			return isSuccess;
		} finally {
			pstmt.close();
		}
		
	}
	
	// 条件查询管理员
	public List<Admin> queryAdmins(List<Map<String, String>> conditionList) throws SQLException {
		StringBuffer sql = new StringBuffer("select a.*, count(b.adminName) amount from admins a");
		sql.append(" left join userlicensereq b on a.adminName = b.adminName where 1=1");
		if(conditionList != null && conditionList.size() > 0) {
			for (Map<String, String> map : conditionList) {
				sql.append(" and ");
				if("privName".equals(map.get("属性"))) {
					sql.append("a.adminName in (");
					sql.append("select adminName from adminprivs where priName like '%").append(map.get("值")).append("%')");
				} else {
					sql.append("a.").append(map.get("属性")).append(" like '%").append(map.get("值")).append("%'");
				}
			}
		}
		sql.append(" group by a.adminName,b.adminName order by a.createDate desc");
		List<Admin> list = null;
		PreparedStatement pstmt = null;
		Connection conn = null;
		ResultSet rs = null;
		Admin admin = null;
		
		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql.toString());
			rs = pstmt.executeQuery();
			list = new ArrayList<Admin>();
			
			while(rs.next()) {
				admin = new Admin();
				admin.setAdminName(rs.getString("adminName"));
				admin.setDeptName(rs.getString("deptName"));
				admin.setPassword(rs.getString("password"));
				admin.setCnName(rs.getString("cnName"));
				admin.setPhone(rs.getString("phone"));
				admin.setStatus(rs.getString("status"));
				admin.setCreateDate(rs.getDate("createDate"));
				admin.setIsSuper(rs.getString("isSuper"));
				admin.setEmail(rs.getString("email"));
				admin.setApproveNum(Integer.parseInt(rs.getString("amount")));
				
				list.add(admin);
			}
			
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return list;
		} finally {
			rs.close();
			pstmt.close();
		} 
	}
	
	// 修改管理员密码
	public boolean editPassword(String adminName, String password) throws SQLException {
		String sql = "update admins set password=? where adminName=?";
		PreparedStatement pstmt = null;
		Connection conn = null;
		isSuccess = false;
		try {
			conn = DBHelper.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, password);
			pstmt.setString(2, adminName);
			
			int i = pstmt.executeUpdate();
			if(i > 0) {
				isSuccess = true;
			}
			return isSuccess;
		} catch (SQLException e) {
			e.printStackTrace();
			return isSuccess;
		} finally {
			pstmt.close();
		}
	}
	
	
	/*
	PreparedStatement pstmt = null;
	Connection conn = null;
	isSuccess = false;
	try {
		conn = DBHelper.getConnection();
		pstmt = conn.prepareStatement(sql);
		int i = pstmt.executeUpdate();
		if(i > 0) {
			isSuccess = true;
		}
		return isSuccess;
	} catch (SQLException e) {
		e.printStackTrace();
		return isSuccess;
	} finally {
		pstmt.close();
	}
	 */
	public AdminDao() {
	}

}
